package com.jgy;

import java.util.Arrays;
import java.util.concurrent.CompletableFuture;

/**
 * @author 姜光远
 * @date 2023/9/8 16:38
 */
public class test {


    public static void main(String[] args) {
        testAsync();
    }

    private static void testAsync() {
        CompletableFuture<Void> voidCompletableFuture1 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("线程1有返回值");
            return 78;
        }).thenAccept(data -> {
            try {
                Thread.sleep(1000);
                System.out.println("数据1查询中.....1");
                Thread.sleep(1000);
                System.out.println("数据1查询中.....2");
                Thread.sleep(1000);
                System.out.println("数据1查询中.....3");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("线程1，没返回值");
            System.out.println(data);
        });

        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            try {
                Thread.sleep(1000);
                System.out.println("数据2查询中.....1");
                Thread.sleep(1000);
                System.out.println("数据2查询中.....2");
                Thread.sleep(1000);
                System.out.println("数据2查询中.....3");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("线程2，没有返回值");
        });

        CompletableFuture<Void> voidCompletableFuture2 = CompletableFuture.allOf(voidCompletableFuture, voidCompletableFuture1);
        voidCompletableFuture2.thenRun(()->{
            voidCompletableFuture.join();
            voidCompletableFuture1.join();
        }).join();


    }
}
